using System;
using UnityEngine;

[Serializable]
[RequireComponent(typeof(CharacterController))]
public class PlayerRelativeControl : MonoBehaviour
{
	public Joystick moveJoystick;

	public Joystick rotateJoystick;

	public Transform cameraPivot;

	public float forwardSpeed;

	public float backwardSpeed;

	public float sidestepSpeed;

	public float jumpSpeed;

	public float inAirMultiplier;

	public Vector2 rotationSpeed;

	private Transform thisTransform;

	private CharacterController character;

	private Vector3 cameraVelocity;

	private Vector3 velocity;

	public PlayerRelativeControl()
	{
		forwardSpeed = 4f;
		backwardSpeed = 1f;
		sidestepSpeed = 1f;
		jumpSpeed = 8f;
		inAirMultiplier = 0.25f;
		rotationSpeed = new Vector2(50f, 25f);
	}

	public void Start()
	{
		thisTransform = (Transform)GetComponent(typeof(Transform));
		character = (CharacterController)GetComponent(typeof(CharacterController));
		GameObject gameObject = GameObject.Find("PlayerSpawn");
		if ((bool)gameObject)
		{
			thisTransform.position = gameObject.transform.position;
		}
	}

	public void OnEndGame()
	{
		moveJoystick.Disable();
		rotateJoystick.Disable();
		enabled = false;
	}

	public void Update()
	{
		Vector3 vector = thisTransform.TransformDirection(new Vector3(moveJoystick.position.x, 0f, moveJoystick.position.y));
		vector.y = 0f;
		vector.Normalize();
		Vector3 zero = Vector3.zero;
		Vector2 vector2 = new Vector2(Mathf.Abs(moveJoystick.position.x), Mathf.Abs(moveJoystick.position.y));
		if (!(vector2.y <= vector2.x))
		{
			if (!(moveJoystick.position.y <= 0f))
			{
				vector *= forwardSpeed * vector2.y;
			}
			else
			{
				vector *= backwardSpeed * vector2.y;
				zero.z = moveJoystick.position.y * 0.75f;
			}
		}
		else
		{
			vector *= sidestepSpeed * vector2.x;
			zero.x = (0f - moveJoystick.position.x) * 0.5f;
		}
		if (!character.isGrounded)
		{
			ref Vector3 reference = ref velocity;
			float y = velocity.y;
			Vector3 gravity = Physics.gravity;
			reference.y = y + gravity.y * Time.deltaTime;
			zero.z = (0f - jumpSpeed) * 0.25f;
			vector.x *= inAirMultiplier;
			vector.z *= inAirMultiplier;
		}
		vector += velocity;
		vector += Physics.gravity;
		vector *= Time.deltaTime;
		character.Move(vector);
		if (character.isGrounded)
		{
			velocity = Vector3.zero;
		}
		Vector3 localPosition = cameraPivot.localPosition;
		localPosition.x = Mathf.SmoothDamp(localPosition.x, zero.x, ref cameraVelocity.x, 0.3f);
		localPosition.z = Mathf.SmoothDamp(localPosition.z, zero.z, ref cameraVelocity.z, 0.5f);
		cameraPivot.localPosition = localPosition;
		if (character.isGrounded)
		{
			Vector2 a = rotateJoystick.position;
			a.x *= rotationSpeed.x;
			a.y *= rotationSpeed.y;
			a *= Time.deltaTime;
			thisTransform.Rotate(0f, a.x, 0f, Space.World);
			cameraPivot.Rotate(a.y, 0f, 0f);
		}
	}

	public void Main()
	{
	}
}
